package com.supermarket.user.controller;

import com.supermarket.common.core.utils.poi.ExcelUtil;
import com.supermarket.common.core.web.controller.BaseController;
import com.supermarket.common.core.web.domain.AjaxResult;
import com.supermarket.common.core.web.page.TableDataInfo;
import com.supermarket.common.log.annotation.Log;
import com.supermarket.common.log.enums.BusinessType;
import com.supermarket.common.security.annotation.RequiresPermissions;
import com.supermarket.user.domain.Tag;
import com.supermarket.user.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

import static com.supermarket.common.core.utils.PageUtils.startPage;
import static com.supermarket.common.core.web.domain.AjaxResult.success;

@RestController
@RequestMapping("/tag")
public class TagController extends BaseController {
    @Autowired
    private TagService tagService;
    /**
     * 查询所有标签
     * @return
     */
    @GetMapping("/search")
    public AjaxResult list(String tagName) {
        List<Tag> list = tagService.list(tagName);
        return success(list);
    }
    /**
     * 查询用户标签列表
     */
    @RequiresPermissions("user:tag:list")
    @GetMapping("/list")
    public TableDataInfo list(Tag tag)
    {
        startPage();
        List<Tag> list = tagService.selectTagList(tag);
        return getDataTable(list);
    }

    /**
     * 导出用户标签列表
     */
    @RequiresPermissions("user:tag:export")
    @Log(title = "用户标签", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Tag tag)
    {
        List<Tag> list = tagService.selectTagList(tag);
        ExcelUtil<Tag> util = new ExcelUtil<Tag>(Tag.class);
        util.exportExcel(response, list, "用户标签数据");
    }

    /**
     * 获取用户标签详细信息
     */
    @RequiresPermissions("user:tag:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(tagService.selectTagById(id));
    }

    /**
     * 新增用户标签
     */
    @RequiresPermissions("user:tag:add")
    @Log(title = "用户标签", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Tag tag)
    {
        return toAjax(tagService.insertTag(tag));
    }

    /**
     * 修改用户标签
     */
    @RequiresPermissions("user:tag:edit")
    @Log(title = "用户标签", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Tag tag)
    {
        return toAjax(tagService.updateTag(tag));
    }

    /**
     * 删除用户标签
     */
    @RequiresPermissions("user:tag:remove")
    @Log(title = "用户标签", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(tagService.deleteTagByIds(ids));
    }
}
